const app = getApp()
module.exports = {
    countHeart: countHeart,
    getIndex0:getIndex0,
    getIndex: getIndex,
    setStorage: setStorage,
    getStorage: getStorage,
    countMaxHeart:countMaxHeart,
    average:average,
    setRestHeart:setRestHeart,

}

let i = 0;
function getIndex0(callback) {
    i++
    i = i%6
    let res = [i, '测量静心心率']
    callback && typeof callback === 'function' ? callback(res) : null;
}

function getIndex(heart, callback) {

    let index = 1;
    if (heart <= app.globalData.hrb.d5) {
        index = 1;
    } else if (heart > app.globalData.hrb.d5 && heart <= app.globalData.hrb.d6) {
        index = 2;
    } else if (heart > app.globalData.hrb.d6 && heart <= app.globalData.hrb.d7) {
        index = 3;
    } else if (heart > app.globalData.hrb.d7 && heart <= app.globalData.hrb.d8) {
        index = 4;
    } else if (heart > app.globalData.hrb.d8 && heart <= app.globalData.hrb.d9) {
        index = 5;
    } else if (heart > app.globalData.hrb.d9) {
        index = 0;
    }
    let msg = '未连接'
    switch (index) {

        case 1:
            msg = '正在测量'
            break;
        case 2:
            msg = '热身运动'
            break;
        case 3:
            msg = '燃脂运动'
            break;
        case 4:
            msg = '有氧运动'
            break;
        case 5:
            msg = '无氧运动'
            break;
        case 0:
            msg = '极限运动'
            break;
    }
    let res = [index, msg]
    callback && typeof callback === 'function' ? callback(res) : null;

}

function setRestHeart(heart) {
    app.globalData.hrb.rest_heart = heart
    wx.setStorageSync('rest_heart', heart)
}

function countMaxHeart() {
    console.log('==计算最大心率')
    if (app.globalData.users != null && app.globalData.users[0].birthday != null && app.globalData.users[0].gender != null) {
        let age = getAge(app.globalData.users[0].birthday)

        if (app.globalData.users[0].gender == '1') {
            app.globalData.hrb.max_heart = Math.round(206.9 - 0.67 * age)
        } else if (app.globalData.users[0].gender == '2') {
            app.globalData.hrb.max_heart = Math.round(206.9 - 0.88 * age)
        }

        console.log(app.globalData.hrb.max_heart)

    }

    countHeart()


}

//计算运动心率
function countHeart() {
    console.log('==计算运动心率')
    console.log(app.globalData.hrb)

    // if (app.globalData.hrb.d5 == 0) {
        app.globalData.hrb.d5 = Math.round((app.globalData.hrb.max_heart - app.globalData.hrb.rest_heart) * 0.5 + app.globalData.hrb.rest_heart);
        wx.setStorageSync('d5', app.globalData.hrb.d5)
    // }
    // if (app.globalData.hrb.d6 == 0) {
        app.globalData.hrb.d6 = Math.round((app.globalData.hrb.max_heart - app.globalData.hrb.rest_heart) * 0.6 + app.globalData.hrb.rest_heart);
        wx.setStorageSync('d6', app.globalData.hrb.d6)
    // }
    // if (app.globalData.hrb.d7 == 0) {
        app.globalData.hrb.d7 = Math.round((app.globalData.hrb.max_heart - app.globalData.hrb.rest_heart) * 0.7 + app.globalData.hrb.rest_heart);
        wx.setStorageSync('d7', app.globalData.hrb.d7)
    // }
    // if (app.globalData.hrb.d8 == 0) {
        app.globalData.hrb.d8 = Math.round((app.globalData.hrb.max_heart - app.globalData.hrb.rest_heart) * 0.8 + app.globalData.hrb.rest_heart);
        wx.setStorageSync('d8', app.globalData.hrb.d8)
    // }
    // if (app.globalData.hrb.d9 == 0) {
        app.globalData.hrb.d9 = Math.round((app.globalData.hrb.max_heart - app.globalData.hrb.rest_heart) * 0.9 + app.globalData.hrb.rest_heart);
        wx.setStorageSync('d9', app.globalData.hrb.d9)
    // }

    console.log(app.globalData.hrb)

}

function setStorage(name, data, callback) {
    console.log('====>>更新缓存')
    console.log(data)
    if (name == 'aim') {
        if (data.aim_min > 0) {
            app.globalData.aim.aim_min = data.aim_min
            wx.setStorageSync('aim_min', data.aim_min)
        }
        if (data.aim_max > 0) {
            app.globalData.aim.aim_max = data.aim_max
            wx.setStorageSync('aim_max', data.aim_max)
        }

    }

    if (name == 'hrb') {
        if (data.rest_heart > 0) {
            app.globalData.hrb.rest_heart = data.rest_heart
            wx.setStorageSync('rest_heart', data.rest_heart)
        }

        if (data.max_heart > 0) {
            app.globalData.hrb.max_heart = data.max_heart
            wx.setStorageSync('max_heart', data.max_heart)
        }
        if (data.d5 > 0) {
            app.globalData.hrb.d5 = data.d5
            wx.setStorageSync('d5', data.d5)
        }
        if (data.d6 > 0) {
            app.globalData.hrb.d6 = data.d6
            wx.setStorageSync('d6', data.d6)
        }
        if (data.d7 > 0) {
            app.globalData.hrb.d7 = data.d7
            wx.setStorageSync('d7', data.d7)
        }
        if (data.d8 > 0) {
            app.globalData.hrb.d8 = data.d8
            wx.setStorageSync('d8', data.d8)
        }
        if (data.d9 > 0) {
            app.globalData.hrb.d9 = data.d9
            wx.setStorageSync('d9', data.d9)
        }


    }

    callback && typeof callback === 'function' ? callback(1) : null;

}

function getStorage(name) {
    console.log('====>>缓存取值')
    if (name == 'hrb') {
        let data = wx.getStorageSync(name)
        if (data.rest_heart > 0) {
            app.globalData.hrb.rest_heart = data.rest_heart
        }

        if (data.max_heart > 0) {
            app.globalData.hrb.max_heart = data.max_heart
        }
        if (data.d5 > 0) {
            app.globalData.hrb.d5 = data.d5
        }
        if (data.d6 > 0) {
            app.globalData.hrb.d6 = data.d6
        }
        if (data.d7 > 0) {
            app.globalData.hrb.d7 = data.d7
        }
        if (data.d8 > 0) {
            app.globalData.hrb.d8 = data.d8
        }
        if (data.d9 > 0) {
            app.globalData.hrb.d9 = data.d9
        }
        console.log(app.globalData.hrb)

    }
    if (name == 'aim') {
        let data = wx.getStorageSync(name)
        if (data.aim_min > 0) {
            app.globalData.aim.aim_min = data.aim_min
        }
        if (data.aim_max > 0) {
            app.globalData.aim.aim_max = data.aim_max
        }

        console.log(app.globalData.aim)
    }


}

/*根据出生日期算出年龄*/
function getAge(strBirthday) {
    var returnAge;
    var strBirthdayArr = strBirthday.split("-");
    var birthYear = strBirthdayArr[0];
    var birthMonth = strBirthdayArr[1];
    var birthDay = strBirthdayArr[2];

    var d = new Date();
    var nowYear = d.getFullYear();
    var nowMonth = d.getMonth() + 1;
    var nowDay = d.getDate();

    if (nowYear == birthYear) {
        returnAge = 0;//同年 则为0岁
    } else {
        var ageDiff = nowYear - birthYear; //年之差
        if (ageDiff > 0) {
            if (nowMonth == birthMonth) {
                var dayDiff = nowDay - birthDay;//日之差
                if (dayDiff < 0) {
                    returnAge = ageDiff - 1;
                } else {
                    returnAge = ageDiff;
                }
            } else {
                var monthDiff = nowMonth - birthMonth;//月之差
                if (monthDiff < 0) {
                    returnAge = ageDiff - 1;
                } else {
                    returnAge = ageDiff;
                }
            }
        } else {
            returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
        }
    }
    return returnAge;//返回周岁年龄
}

function average(nums) {
    return Math.round(nums.reduce((a, b) => a + b) / nums.length);
}